package top.fzqblog.clazz

import kotlin.properties.Delegates
import kotlin.reflect.KProperty

/**
 * Created by 抽离 on 2017/8/2 0002.
 * 自定义委托
 */
class Example {
    //var msg: String by Delegate()
    var msg: String by Delegates.observable("",  {
        kProperty, oldValue, newValue ->
        println("属性：" + kProperty + "---oldValue：" + oldValue + "---newValue：" + newValue);
    })
}

class Delegate {

    private var message = "Default Message"

    operator fun getValue(thisRef: Any?, prop: KProperty<*>): String {
        return "${prop.name} = $message from $thisRef"
    }

    operator fun setValue(thisRef: Any?, prop: KProperty<*>, value: String) {
        message = value
    }
}

fun main(args: Array<String>) {
    val e = Example()
    println(e.msg)  //  msg = Default Message
    e.msg = "New Message"
    println(e.msg)  //  msg = New Message
}

